package com.aier.proxy.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin.entity.SysCity;
import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.common.dao.ICommonDAO;
import com.aier.entrance.entity.EntranceDevice;
import com.aier.entrance.service.IEntranceCardService;
import com.aier.proxy.comm.ProxyEquipPageSize;
import com.aier.proxy.service.IProxyService;
import com.aier.proxy.service.IUserCreateSchoolService;
import com.aier.school.entity.School;
import com.aier.school.entity.SchoolInfoDetail;
import com.aier.school.service.ISchoolService;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("proxyAssignEquipAction")
public class ProxyAssignEquipAction extends ProxyBaseAction {
    /**
     * 分派设备页面
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "userCreateSchoolService")
    private IUserCreateSchoolService createSchoolService;

    @Resource(name = "schoolService")
    private ISchoolService schoolService;

    @Resource(name = "userService")
    private IUserService userService;
    
    @Resource(name = "proxyService")
    private IProxyService proxyService;
    
    @Resource(name = "entranceCardService")
    private IEntranceCardService entranceCardService;
    
    private Long eid;

    private Integer pid;

    private Integer pcid;
    // 超级管理员账号
    private String aeAccount;
    // 超级管理员姓名
    private String aeName;

    // 学校编号
    private Integer sid;

    private List<School> schools;

    private List<SysCity> sysCities;
    // 学校详细信息
    private SchoolInfoDetail schoolInfo;

    private int s;

    // 页数
    private int page;
    // 每页显示记录数
    private int page_size;
    // 总页数
    private int page_count;
    // 总数
    private int count;
    // 排序方式
    private String sord;
    // 排序字段
    private String sort;

    private List<EntranceDevice> eds;
    
    @Override
    public String execute() {

        return SUCCESS;
    }

    /**
     * 查找代理下的城市
     * 
     * @return
     */
    public String searchProxyCity() {
        sysCities = createSchoolService.searchProxyCity1(pid);
        if (sysCities != null && sysCities.size() > 0) {
            s = Constant.num_10001;
        } else {
            s = Constant.num_10000;
        }
        return SUCCESS;
    }

    /**
     * 查询代理学校
     * 
     * @return
     */
    public String searchSchool() {
        if (null != pcid && null != pcid) {
            schools = createSchoolService.searchSchool(pid, pcid);
            if (schools != null && schools.size() > 0) {
                s = Constant.num_10001;
            } else {
                s = Constant.num_10000;
            }
            return SUCCESS;
        } else {
            return SUCCESS;
        }
    }

    /**
     * 查询学校信息
     * 
     * @return
     */
    public String searchSchoolInfo() {
        schoolInfo = schoolService.searchSchoolInfoBySid(sid);
        s = Constant.num_10001;
        return SUCCESS;
    }

    /**
     * 根据用户账号取得姓名
     * 
     * @return
     */
    public String searchNameByAccount() {
        User u = userService.searchUserByCondition(aeAccount);
        try {
            aeName = userService.getUserByUid(u.getUid()).getRealname();
        } catch (Exception e) {
            s = Constant.num_10002;
            return SUCCESS;
        }
        if (null == aeName || aeName.equals("")) {
            s = Constant.num_10000;
            return SUCCESS;
        }

        s = Constant.num_10001;
        return SUCCESS;
    }

    /**
     * 查询门禁设备信息
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchEntranceInfo() {
        
        if(page<=0){
            page=1;
        }
        int sortable=-1;
        if("sn".equals(sort)){
            sortable = ("asc".equals(sord))?IProxyService.ENTRANCE_SORTABLE_SN:IProxyService.ENTRANCE_SORTABLE_SN_DESC;
        }
        Map<String,Object> map = proxyService.searchEntDevicesByCondition(page, 
                ProxyEquipPageSize.PROXY_ENTRANCE_PAGESIZE, sortable, pid, null, null, null, null);
        if(null==map){
            s=Constant.num_1000;
            return ERROR;
        }
        eds = (List<EntranceDevice>) map.get(ICommonDAO.OBJ_LIST);
        if (null == eds) {
            s = 0;
            return SUCCESS;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        return SUCCESS;

    }

    /**
     * 分配设备给学校
     * @return
     */
    public String assign(){
        s=(entranceCardService.updateAssignEntrance(eid, sid, (Long)session.get(CommonSession.SESSION_ACCOUNT_ID))>0)?1:0;
        return SUCCESS;
        
    }
    
    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public Integer getPcid() {
        return pcid;
    }

    public void setPcid(Integer pcid) {
        this.pcid = pcid;
    }

    public List<School> getSchools() {
        return schools;
    }

    public List<SysCity> getSysCities() {
        return sysCities;
    }

    public SchoolInfoDetail getSchoolInfo() {
        return schoolInfo;
    }

    public int getS() {
        return s;
    }

    public void setCreateSchoolService(
            IUserCreateSchoolService createSchoolService) {
        this.createSchoolService = createSchoolService;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public void setAeAccount(String aeAccount) {
        this.aeAccount = aeAccount;
    }

    public String getAeName() {
        return aeName;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public int getCount() {
        return count;
    }

    public int getPage_count() {
        return page_count;
    }

    public int getPage_size() {
        return page_size;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public List<EntranceDevice> getEds() {
        return eds;
    }

    public void setProxyService(IProxyService proxyService) {
        this.proxyService = proxyService;
    }

    public void setEntranceCardService(IEntranceCardService entranceCardService) {
        this.entranceCardService = entranceCardService;
    }

    public void setEid(Long eid) {
        this.eid = eid;
    }

}
